<?php
class httpdown{
	
	private $m_fp;
	private $m_host;
	private $m_url;
	private $m_path;
	private $m_query;
	private $m_port;
	private $m_error;
	private $m_httphead;
	private $m_puthead;
	private $m_count = 0;
	private $_forms;
	private $_files;
	private $_filecontents;
	private $_cookies;
	
	public function connect($url){
		$this->reset();
		$this->init($url);	
		$this->run();	
	}
	
	public function add_form($key,$val){
		$this->_forms[$key] = $val;
	}
	public function add_filecontents($key,$filename,$filecontent){
		$this->_filecontents[$key]['filename'] = $filename;
		$this->_filecontents[$key]['filecontent'] = $filecontent;
	}
	public function add_file($key,$filename){
		$this->_files[$key] = $filename;
	}
	
	public function getraw(){
		if($this->m_fp){
			while(!feof($this->m_fp)){
				$s = fgets($this->m_fp,256);				
			}
			return $s;
		}
	}
	
	public function connect_status(){
		if($this->m_fp){
			$s = fgets($this->m_fp,256);
			$info = explode(' ',$s);
			return $info[1];
		}
	}
	
	private function run(){
		//检测$this->m_host
		if("" == $this->m_host){
			return ;
		}
		//尝试建立fsocket连接
		if(!$this->open_host()){
			$this->print_error();
			return ;
		}
		//请求类型
		$method = (count($this->_forms)+count($this->_files)+count($this->_filecontents))>0?'POST':'GET';
		$send_head = "{$method} {$this->m_path} HTTP/1.1\r\n";
		//发送那个第一个请求头
		fwrite($this->m_fp, $send_head);
		
		//$out .= $send_head;
		
		//设置Content-Type 和 $boundary \ $boundary_2
		$boundary = $boundary_2 ="";
		if("POST" == $method){
			if(count($this->_files)>0 || count($this->_filecontents)){					
				$boundary = "###";
				$boundary_2 =  "--{$boundary}";
				$this->m_puthead["Content-Type"] = "multipart/form-data; boundary={$boundary}";
			}else{
				$this->m_puthead["Content-Type"] = "application/x-www-form-urlencoded";
			}
		}
		//配置其他请求头信息
		$this->m_puthead["Accept"] = "text/html, application/xhtml+xml, */*";
		$this->m_puthead["Accept-Language"] = "zh-CN";
		$this->m_puthead["User-Agent"] = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
		$this->m_puthead["Accept-Encoding"] = "*";
		$this->m_puthead["Referer"] = "http://".$this->m_host.$this->m_path;
		$this->m_puthead["Host"] = $this->m_host;
		
		//遍历输出请求头信息
		foreach($this->m_puthead as $key=>$val){
			$key = trim($key);
			$val = trim($val);
			if( ""!=$key && ""!=$val ){
				$send_head = $key.": ".$val."\r\n";
				fwrite($this->m_fp, $send_head);
				//$out .= $send_head;
			}			
		}
		
		//输出cookie信息
		if(!empty($this->_cookies)){
			$send_head = "Cookie: ".join(';',$this->_cookies)."\r\n";
			fwrite($this->m_fp,$send_head);	
			//$out .= $send_head;		
		}
		
		
			
		//整理文件内容
		$contents = "";
		if( 0==count($this->_files) && 0==count($this->_filecontents) && 0<count($this->_forms) ){
			foreach($this->_forms as $key=>$val){			
				if(""==$contents){
					$contents .= "{$key}=".urlencode($val);
				}else{
					$contents .="&$key=".urlencode($val);
				}
			}
		}elseif( 0<count($this->_files) || 0<count($this->_filecontents) ){
			if( 0<count($this->_forms) ){
				foreach($this->_forms as $key=>$val){
					$contents .= $boundary_2."\r\nContent-Disposition: form-data; name=\"{$key}\"\r\n\r\n";
					$contents .= rawurldecode($val)."\r\n";				
				}
			}
			if( 0<count($this->_files) ){
				foreach($this->_files as $key=>$filename){
					$contents .= $boundary_2."\r\nContent-Disposition: form-data; name=\"{$key}\"; filename=\"{$filename}\"\r\n";
					$contents .= "Content-Type: text/plain\r\n\r\n";
					$contents .= file_get_contents($filename);
					$contents .= "\r\n".$boundary_2."--\r\n\r\n";
				}
			}
			if( 0<count($this->_filecontents) ){
				foreach($this->_filecontents as $key=>$fileinfo){
					$contents .= $boundary_2."\r\nContent-Disposition: form-data; name=\"{$key}\"; filename=\"{$fileinfo['filename']}\"\r\n";
					$contents .= "Content-Type: text/plain\r\n\r\n";
					$contents .= $fileinfo['filecontent'];
					$contents .= "\r\n".$boundary_2."--\r\n\r\n";
				}
			}
		}
		//清空数据容器
		$this->_cookies = $this->_forms = $this->_files = $this->_filecontents = array();
	
		//根据contents计算数据长度，发送请求头
		$send_head = "Content-Length: ".strlen($contents)."\r\n";
		fwrite($this->m_fp, $send_head);
		
		//$out .= $send_head;
		//发送连接状态
		$send_head = "Connection: Keep-Alive\r\n\r\n";
		fwrite($this->m_fp, $send_head);
		
		//$out .= $send_head;
		//没缓存
		//$send_head = "Cache-Control: no-cache\r\n\r\n";
		//fwrite($this->m_fp, $send_head);
		
		//$out .= $contents;		
		//file_put_contents('httpdown.log',$out );
		
		//发送数据
		fwrite($this->m_fp, $contents);
		
	}
	private function open_host(){
		if("" == $this->m_host){
			return false;
		}
		$this->m_fp = @fsockopen($this->m_host,$this->m_port,$errno,$errstr,3);
		if(!$this->m_fp){
			$this->m_error = "错误编号：".$errno."，错误信息：".$errstr;
			return false;
		}
		return true;
	}
	
	private function print_error(){
		echo $this->m_error;
	}
	
	private function init($url){
		if("" == $url){
			return ;
		}
		$urls = @parse_url($url);
		$this->m_url = $url;
		$this->m_host = $urls['host'];		
		if(!empty($urls['path'])){
			$this->m_path = $urls['path'];
		}
		if(!empty($urls['query'])){
			$this->m_query = $urls['query'];
		}
		if(!empty($urls['m_port'])){
			$this->m_port= $urls['m_port'];
		}		
	}
	
	private function reset(){
		$this->m_count++;
		$this->m_host = "";
		$this->m_url = "";
		$this->m_path = "/";
		$this->m_port = "80";
		$this->m_query = "";
		$this->m_error = "";
		$this->m_httphead = "";
		$this->m_puthead = "";
		$this->close();
	}
	
	private function close(){
		if($this->m_fp){
			@fclose($this->m_fp);
		}
	}
	
}
?>